package com.heima.search.service.impl;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.search.dtos.UserSearchDto;
import com.heima.model.search.pojos.ApAssociateWords;
import com.heima.search.service.ApAssociateWordsService;
import com.heima.search.utils.Trie;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class ApAssociateWordsServiceImpl implements ApAssociateWordsService, InitializingBean {
    @Autowired
    MongoTemplate mongoTemplate;

    /**
     * 联想词
     *
     * @param userSearchDto
     * @return
     */
    @Override
    public ResponseResult findAssociate(UserSearchDto userSearchDto) {
        //检查参数
        if (userSearchDto == null || StringUtils.isBlank(userSearchDto.getSearchWords())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //执行查询 模糊查询
        List<ApAssociateWords> wordsList = mongoTemplate.find(Query.query(Criteria.where("associateWords").regex(".*" + userSearchDto.getSearchWords() + ".*"))
                , ApAssociateWords.class);
        return ResponseResult.okResult(wordsList);
    }

    public static volatile SoftReference<Trie> associateWordsTrie = null;

    @Override
    public ResponseResult findAssociateV2(UserSearchDto userSearchDto) {
        //参数检查
        if (userSearchDto == null || StringUtils.isBlank(userSearchDto.getSearchWords())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (associateWordsTrie == null || associateWordsTrie.get() == null) {
            initTrieData();
        }
        Trie trie = associateWordsTrie.get();
        List<String> words = trie.getData(userSearchDto.getSearchWords());
        List<ApAssociateWords> apAssociateWordList = new ArrayList<>();
        if (words != null && words.size() != 0) {
            for (String word : words) {
                ApAssociateWords apAssociateWords = new ApAssociateWords();
                apAssociateWords.setAssociateWords(word);
                apAssociateWordList.add(apAssociateWords);
            }
        }
        return ResponseResult.okResult(apAssociateWordList);
    }

    //初始化trie树
    private void initTrieData() {
        Trie trie = new Trie();
        // 查询联想词
        List<ApAssociateWords> associateWords =
                mongoTemplate.findAll(ApAssociateWords.class);
        if (associateWords != null && associateWords.size() != 0) {
            for (ApAssociateWords associateWord : associateWords) {
                String words = associateWord.getAssociateWords();
                trie.insert(words);
            }
        }
        SoftReference<Trie> trieSoftReference = new SoftReference<>(trie);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("初始化tree树");
        log.info("初始化tree树开始 ======> {}");
        initTrieData();
    }
}